﻿using System;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace CKS.SharePoint.Workbook.Community {
    public class GenericListActivityEventReceiver : SPItemEventReceiver {
        public override void ItemAdded(SPItemEventProperties properties) {
            this.DisableEventFiring();
            base.ItemAdded(properties);

            WriteLog(properties, "User has added a new item");
            this.EnableEventFiring();
        }

        private static void WriteLog(SPItemEventProperties properties, string message) {
            SPWeb web = properties.ListItem.Web;
            SPSite site = web.Site;
            SPUser user = web.AllUsers[properties.UserLoginName];
            ActivityLogger logger = new ActivityLogger(web);
            string fullUrl = SPUtility.GetFullUrl(site, "/" + properties.ListItem.Url);
            logger.Write(message, ActivityType.UserAddedContent, user, fullUrl);
        }

        public override void ItemCheckedIn(SPItemEventProperties properties) {
            this.DisableEventFiring();
            base.ItemCheckedIn(properties);
            WriteLog(properties, "User has checked in an item");
            this.EnableEventFiring();

        }

        public override void ItemUpdated(SPItemEventProperties properties) {
            this.DisableEventFiring();
            base.ItemUpdated(properties);
            WriteLog(properties, "User has updated an item");
            this.EnableEventFiring();
        }
    }
}
